package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.dto.SpecValuesDTO;
import com.example.entity.SpecValuesEntity;
import com.example.model.page.PageData;
import com.example.mybatis.service.impl.CrudServiceImpl;
import com.example.dao.SpecNamesDao;
import com.example.dto.SpecNamesDTO;
import com.example.entity.SpecNamesEntity;
import com.example.service.SpecNamesService;
import com.example.service.SpecValuesService;
import com.example.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 规格名称表
 *
 * @author yyyyjinying 1020066395@qq.com
 * @since 1.0-SNAPSHOT 2025-04-29
 */
@Service
public class SpecNamesServiceImpl extends CrudServiceImpl<SpecNamesDao, SpecNamesEntity, SpecNamesDTO> implements SpecNamesService {
    @Autowired
    private SpecValuesService specValuesService;
    @Override
    public QueryWrapper<SpecNamesEntity> getWrapper(Map<String, Object> params) {
        QueryWrapper<SpecNamesEntity> wrapper = new QueryWrapper<>();

        String specName = (String) params.get("specName");
        wrapper.like(StringUtils.isNotBlank(specName), "spec_name", specName);

        return wrapper;
    }

    @Override
    public void save(SpecNamesDTO dto) {
        super.save(dto);
        List<SpecValuesDTO> specValues = dto.getSpecValues();
        specValues.forEach(specValuesDTO -> {
            specValuesDTO.setSpecId(dto.getSpecId());
        });
        List<SpecValuesEntity> specValuesEntities = ConvertUtils.sourceToTarget(specValues, SpecValuesEntity.class);
        specValuesService.insertBatch(specValuesEntities);
    }

    @Override
    public List<SpecNamesDTO> list(){
        List<SpecNamesEntity> specNamesEntities = baseDao.selectList(null);
        return ConvertUtils.sourceToTarget(specNamesEntities, SpecNamesDTO.class);
    }

    @Override
    public void update(SpecNamesDTO dto) {
        super.update(dto);
        specValuesService.deleteBySpecId(dto.getSpecId());
        List<SpecValuesEntity> specValuesEntities = ConvertUtils.sourceToTarget(dto.getSpecValues(), SpecValuesEntity.class);
        specValuesService.insertBatch(specValuesEntities);
    }

    public SpecNamesDTO get(Long id) {
        SpecNamesDTO specNamesDTO = super.get(id);
        List<SpecValuesEntity> values = specValuesService.getBySpecId(specNamesDTO.getSpecId());
        List<SpecValuesDTO> specValuesDTOS = ConvertUtils.sourceToTarget(values, SpecValuesDTO.class);
        specNamesDTO.setSpecValues(specValuesDTOS);
        return specNamesDTO;
    }

    @Override
    public PageData<SpecNamesDTO> page(Map<String, Object> params) {
        // 动态拼接sql ORDER BY created_at DESC LIMIT ?
        IPage<SpecNamesEntity> page = getPage(params, null, false);
        List<SpecNamesEntity> dishesEntities = baseDao.getPage(params);
        page.setRecords(dishesEntities);
        return getPageData(page, SpecNamesDTO.class);
    }


}